package com.lf.service.impl;

import com.lf.service.EmailService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.CharEncoding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.apache.dubbo.config.annotation.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Slf4j
@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private JavaMailSenderImpl javaMailSender;
    @Value("${spring.mail.username}")
    private String emailUserName;


    @Override
    public void sendEmail(String email, String code) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage, false, CharEncoding.UTF_8);
            message.setTo(email);
            message.setFrom(emailUserName);
            message.setSubject("验证码校验");
            String text = "验证码为: " + code + "，10分钟内有效，请妥善保管!";
            message.setText(text);
            javaMailSender.send(mimeMessage);
            log.debug("send e-mail to {},code is {}",email,code);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
